package com.asnike.net{
	import com.asnike.external.json.JSON;
	
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.errors.IOError;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;
	import flash.system.Security;
	import flash.system.SecurityDomain;
	
	public class ASLoader{
		static private var TYPE_XML : String = 'xml';
		static private var TYPE_IMAGE : String = 'image';
		static private var TYPE_JSON : String = 'json';
		
		static public function loadImage( $urls:*, $FN_Array:Function , $FN_Number:Function=null):void{
			load( $urls, $FN_Array , $FN_Number, TYPE_IMAGE );
		}
		
		static public function loadXML($urls:*, $FN_Array:Function , $FN_Number:Function=null):void{
			load( $urls, $FN_Array , $FN_Number, TYPE_XML );		
		}
		static public function loadJSON($urls:*, $FN_Array:Function , $FN_Number:Function=null):void{
			load( $urls, $FN_Array , $FN_Number, TYPE_JSON );		
		}
		
		
		static private function load($urls:*, $FN_Array:Function , $FN_Number:Function=null, $type:String = ''):void{
			var url:String, currentCount:int, totalCount:int, results:Array, loader:*, dispatcher:*, context:LoaderContext;
			
			if( Security.sandboxType === Security.REMOTE ){
				context = new LoaderContext( false, null, SecurityDomain.currentDomain );
			}
			
			switch( $type ){
			case TYPE_IMAGE:
				loader = new Loader;
				dispatcher = loader.contentLoaderInfo;
				break;
			case TYPE_XML:case TYPE_JSON:
				loader = new URLLoader;
				dispatcher = loader;
				break;
			}
			
			results = [];
			
			if( $urls is String ){
				totalCount = 1;
				url = $urls;
			}else if( $urls is Array ){
				totalCount = $urls.length;
				url = $urls[currentCount];
			}else{
				throw new Error( '!!!' );
			}
			
			load( url );
			
			function load( $url:String ):void{
				if($FN_Number != null){
					dispatcher.addEventListener( ProgressEvent.PROGRESS, function( $e:ProgressEvent ):void{
						$FN_Number( $e.bytesLoaded/$e.bytesTotal );
					} );
				}
				
				dispatcher.addEventListener(IOErrorEvent.IO_ERROR, function( $e:IOErrorEvent ) :void{
					trace('IOError :' + $e.text);
				} );
				
				
				dispatcher.addEventListener( Event.COMPLETE, function( $e:Event ):void{
					results[results.length] = getResult( $e.target, $type );
					++currentCount;
					
					$e.target.removeEventListener( $e.type, arguments.callee );
					
					if( currentCount < totalCount ){
						load( $urls[currentCount] );
					}else{
						$FN_Array( results );
					}
				} );
				loader.load( new URLRequest( $url ) );
			}
		}
		static private function getResult( $target:*, $type:String ):*{
			if( $target is LoaderInfo ){
				return $target.content.bitmapData.clone();
			}else if( $target is URLLoader && $type === TYPE_XML ){
				return XML($target.data);	
			}else if( $target is URLLoader && $type === TYPE_JSON ){
				return JSON.decode( $target.data );
			}
			
			return null;
		}
		
		public function ASLoader(){
			throw new Error( 'NMLoader 클래스는 생성할 수 없습니다.' );
		}
	}
}